{-# LANGUAGE TemplateHaskell #-}

module Shader where

import GHCJS.DOM.WebGLRenderingContextBase
import GHCJS.DOM.Types hiding (Text)

import Data.Text (Text)
import Data.FileEmbed (embedFile)
import Data.Text.Encoding (decodeUtf8)

vertShaderSource :: Text
vertShaderSource = decodeUtf8 $(embedFile "shader/graph.vert")

fragShaderSource :: Text
fragShaderSource = decodeUtf8 $(embedFile "shader/graph.frag")

initShader :: IsWebGLRenderingContextBase self => self -> IO (Maybe WebGLProgram)
initShader gl = do
  frg <- createShader gl FRAGMENT_SHADER
  shaderSource gl frg fragShaderSource
  compileShader gl frg

  vert <- createShader gl VERTEX_SHADER
  shaderSource gl vert vertShaderSource
  compileShader gl vert

  prog <- createProgram gl
  attachShader gl prog frg
  attachShader gl prog vert
  linkProgram gl prog
  useProgram gl prog
  return prog
